package com.eascs.applyLoan.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.applyLoan.dao.O2oLoanInfoDao;
import com.eascs.applyLoan.entity.O2oLoanInforEntity;
import com.eascs.applyLoan.service.O2oLoanInfoService;

@Service
public class O2oLoanInfoServiceImpl implements O2oLoanInfoService {
	@Autowired
	O2oLoanInfoDao o2oLoanInfoDao;

	@Override
public PageResult<Object[]> findByPage(O2oLoanInforEntity oe) {
		
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, oe.getPageSize(), oe.getPageIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT ai.id,DATE_FORMAT(ai.create_time,'%Y-%m-%d %H:%i'), ai.uname ,CONCAT( LEFT (ai.id_card, 3), '****', RIGHT (ai.id_card, 3)) , ai.telphone , CAST(ai.loan_money AS signed),  ");
		querySql.append(" ai.loan_time , ai.loan_type,oi.office_name , DATE_FORMAT(oi.register_time,'%Y-%m-%d') , oi.province , oi.city , ");
		querySql.append(" oi.detail_addresss , oi.cooperate_platform , oi.cooperate_time, lm.uname as chargeMan,ai.credit_manager,lm.employee_code ");
		querySql.append(" from ( (applay_info ai LEFT JOIN office_info oi on ai.id = oi.applyer_id) LEFT JOIN loan_linkman lm on oi.province = lm.province) WHERE 1=1  ");

		if (!StringUtil.isEmpty(oe.getProvince())) {
			querySql.append(" AND oi.province = ? ");
			sqlPageQuery.addParam(oe.getProvince());
		}
		if (!StringUtil.isEmpty(oe.getCity())) {
			querySql.append(" AND oi.city = ? ");
			sqlPageQuery.addParam(oe.getCity());
		}
		if (!StringUtil.isEmpty(oe.getStartDate())) {
			
			querySql.append(" AND ai.create_time >= ? ");
			sqlPageQuery.addParam(oe.getStartDate());
		}
		if (!StringUtil.isEmpty(oe.getEndDate())) {
			querySql.append(" AND ai.create_time < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(oe.getEndDate()), 1)));
		}
		querySql.append(" ORDER BY ai.create_time DESC");
		sqlPageQuery.setQuerySql(querySql.toString());
		return o2oLoanInfoDao.findDataWithPage(sqlPageQuery);
	}

	


}
